<template>
  <div class="my_person">
    <el-card style="width: 55%;">
      <el-form :model="user" label-width="80px" :rules="rules">

        <div style="margin-bottom: 15px; text-align: center;">
          <el-upload
            class="avatar-uploader"
            :action="$baseUrl + '/file/upload'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :headers="{ token: user.token }">
            <img v-if="user.avatar" :src="user.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>


        <el-form-item label="用户名" prop="username">
          <el-input v-model="user.username" disabled placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="user.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="user.phone" placeholder="请输入电话"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="user.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input  
            v-model="user.address" 
            placeholder="请输入地址"
            type="textarea"
            :rows="2"
          ></el-input>
        </el-form-item>

        <div style="text-align: center;margin-bottom: 20px;">
          <el-button type="primary" @click="update">保 存</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>


<script>


export default {
  data() {

    // 手机号校验
    const validateCode = (rule, value, callback) => {
      const regex = /^1[3456789]\d{9}$/;
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else if (!regex.test(value)) {
        callback(new Error('手机号输入错误'))
      } else {
        callback()
      }
    }
    return {
      user: JSON.parse(localStorage.getItem('honey-user') || '{}'),

      

      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' },
        ],
      }
    }
  },
  methods: {
    handleAvatarSuccess(response, file, fileList) {
      console.log(response, file, fileList);
      // 将user的头像属性换成上传的图片的链接
      this.user.avatar = response.data;
    },
    update() {
      //保存数据到数据库
      this.$request.put('/user/update', this.user).then(res => {
        if (res.code == 200) {
          this.$message.success('保存成功')
          localStorage.setItem('honey-user', JSON.stringify(this.user))
          // 向父组件发送自定义事件，并传值
          this.$emit('update:user', this.user)
          
        } else {
          this.$message.error(res.msg)
        }
      })
      //更新数据
    }
  }
}
</script>

<style scope>
.my_person ::v-deep .el-form-item__label {
  font-weight: bold;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    border-radius: 50%;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border-radius: 50%;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    border-radius: 50%;
  }
</style>